use diesel::prelude::*;
use diesel::MysqlConnection;

use super::model::*;
use crate::database::{crud, schema::category, DbError};
use crate::{db_create, db_delete, db_read_by_field, db_read_count_from_offset, db_update};

db_create!(category, Category, NewCategory, create);
db_read_count_from_offset!(category, Category, read_count_from_offset);
db_read_by_field!(category, Category, read_by_slug, slug, String);
db_update!(category, NewCategory, update);
db_delete!(category, delete);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        database::establish_connection, db_read, test_db_create, test_db_delete, test_db_update,
    };

    db_read!(category, Category, read);

    fn insert_test_data(conn: &mut MysqlConnection) -> (i32, i32, i32) {
        let cid1 = create(
            conn,
            NewCategory {
                name: "CATE1",
                desc: "DESC1",
                cover: "COVER1",
                slug: "LINK1",
            },
        )
        .unwrap();
        let cid2 = create(
            conn,
            NewCategory {
                name: "CATE2",
                desc: "DESC2",
                cover: "COVER2",
                slug: "LINK2",
            },
        )
        .unwrap();
        (cid1, cid2, 0)
    }

    test_db_create!();
    test_db_update!(name, "NEW_CATE".to_string());
    test_db_delete!();
}
